from django.shortcuts import render
from django.core.paginator import Paginator,EmptyPage
from django.views import View
from goods.models import SKU,GoodsCategory
from django.http import JsonResponse
from .utils import get_breadcrumb
from haystack.views import SearchView
# Create your views here.

class ListView(View):
    def get(self,request,category_id):
        page = request.GET.get('page')
        page_size = request.GET.get('page_size')
        ordering = request.GET.get('ordering')


        try:
            category = GoodsCategory.objects.get(id=category_id)
        except Exception as e:
            return JsonResponse({
                'code':400,
                'errmsg':'获取mysql数据库错误'
            })

        breadcrumb = get_breadcrumb(category)

        try:
            skus = SKU.objects.filter(
                category=category,
                is_launched=True
            ).order_by(ordering)
        except Exception as e:
            return JsonResponse({
                'code':400,
                'errmsg':'获取mysql数据错误'
            })
        paginator = Paginator(skus,page_size)
        try:
            page_skus = paginator.page(page)
        except Exception as e:
            return JsonResponse({
                'code':400,
                'errmsg':'page数据错误'
            })
        # total_page = paginator.num_pages
        list = []
        for sku in page_skus:
            list.append({
                'id':sku.id,
                'default_image_url':sku.default_image_url,
                'name':sku.name,
                'price':sku.price
            })
        category = GoodsCategory.objects.get(pk=category_id)
        breadcrumb = get_breadcrumb(category)
        return JsonResponse({
            'code':0,
            'errmsg':'ok',
            'breadcrumb':breadcrumb,
            'list':list,
            'count':paginator.num_pages
        })

class HotGoodsView(View):
    def get(self,request,category_id):
        try:
            skus = SKU.objects.filter(
                category_id=category_id,
                is_launched=True
            ).order_by('-sales')[:2]
        except Exception as e:
            return JsonResponse({
                'code':400,
                'errmsg':'获取商品错误'
            })
        hot_skus = []
        for sku in skus:
            hot_skus.append({
                'id':sku.id,
                'default_image_url':sku.default_image_url,
                'name':sku.name,
                'price':sku.price
            })
        return JsonResponse({
            'code':0,
            'errmsg':'ok',
            'hot_skus':hot_skus
        })

class MySearchView(SearchView):
    def create_response(self):
        context = self.get_context()
        data_list = []
        for sku in context['page'].object_list:
            data_list.append({
                'id':sku.object.id,
                'name':sku.object.name,
                'price': sku.object.price,
                'default_image_url': sku.object.default_image_url,
                'searchkey': context.get('query'),
                'page_size': context['page'].paginator.num_pages,
                'count': context['page'].paginator.count
            })
        return JsonResponse(data_list,safe=False)